/*
 * Copyright (C) 2000-2008 Jens Nixdorf and various contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.mopore.jat.board;

import java.awt.Color;
import java.awt.Point;

public class Marking {

    private Point start;

    private Point[] coords;

    private int index;

    private Integer size;

    private Color color;

    public Marking(Point point, Color color, Integer size) {
        this.start = point;
        this.index = 0;
        this.coords = new Point[1000];
        this.color = color;
        this.size = size;
    }

    /**
     * @return Returns the color.
     */
    public Color getColor() {
        return color;
    }

    /**
     * @return Returns the size.
     */
    public Integer getSize() {
        return size;
    }

    public Point getStart() {
        return start;
    }

    public void add(Point point) {
        assertSize();
        // TODO check sound activated Flag
        // optimize strategy for saving points
        // remove the middle point, if there are 2 others
        // laying on the same line (x or y) before and afterwards
        // (P1)-----------(_removeMe_)------------(P3)

        if (index > 2) {

            int p1x = coords[index - 2].x;
            int p1y = coords[index - 2].y;

            int p2x = coords[index - 1].x;
            int p2y = coords[index - 1].y;

            int p3x = point.x;
            int p3y = point.y;

            // on a h-line
            if (p1x == p2x && p2x == p3x) {
                coords[index - 1] = point;
                return;
            }

            // on a v-line
            if (p1y == p2y && p2y == p3y) {
                coords[index - 1] = point;
                return;
            }
        }

        coords[index] = point;
        index++;
    }

    private void assertSize() {

        int length = coords.length;
        boolean isToSmall = (index == length);

        if (isToSmall) {
            int newLength = length + 1000;
            Point[] coords = new Point[newLength];
            System.arraycopy(this.coords, 0, coords, 0, length);
            this.coords = coords;
        }
    }

    public Point[] getCoords() {
        return coords;
    }
}
